#include "giza.h"

void usage()
{
	fprintf( stderr, "usage: crossnum  giza-file\n" );
}

bool convert( char *giza_fn )
{
	CGiza giza;
	if( giza.Open( giza_fn ) == false )	{
		fprintf( stderr, "Giza file %s fail\n", giza_fn );
		return false;
	}

	///////////
	int num_sent = 0;
	int num_align = 0;
	int num_cross = 0;
	while(1)	{
		if( giza.ReadSent() == false )	{
			break;
		}
		num_sent++;


		/*
		if( num_sent != 4 )	continue;
		if( num_sent > 30 )	break;

		giza.align.align_list.PrintEx( stdout, "  " ); fprintf( stdout, "\n" );
		*/
		// make pair list
		CUStringList l;
		for( int i=0; i< giza.align.align_list.GetSize(); i++ )	{
			CUString str = giza.align.align_list.GetAt(i);
			if( str.IsEmpty() )	continue;

			CUStringList idlist;
			idlist.TokenizeByChar( str, " ", false );

			for( int j=0; j<idlist.GetSize(); j++ )	{
				int a=atoi( idlist.GetAt(j) );
				char buf[1024];
				sprintf( buf, "%d-%d", i, a );
				l.PushBack( buf );
				num_align++;
			}


		}

		for( int i=0; i<l.GetSize(); i++ )	{
			CUString str = l.GetAt(i);
			CUStringList t; t.TokenizeByChar( str, "-", false );
			int src_id = atoi(t.GetAt(0) );
			int tgt_id = atoi(t.GetAt(1) );

			for( int j=0; j<l.GetSize(); j++ )	{
				if( i == j )	continue;

				bool have_cross = false;
				CUString str2 = l.GetAt(j);
				CUStringList t2; t2.TokenizeByChar( str2, "-", false );
				int src_id2 = atoi(t2.GetAt(0) );
				int tgt_id2 = atoi(t2.GetAt(1) );


				if( src_id < src_id2 && tgt_id > tgt_id2 )	{
					have_cross = true;
				}
				if( src_id > src_id2 && tgt_id < tgt_id2 )	{
					have_cross = true;
				}

				if( have_cross == true )	{
					//fprintf( stdout, " !cross %d - %d\n", src_id, tgt_id );
					num_cross++;
					break;
				}

			}

		}



		/*

		// line1
		fprintf( out_file.GetFP(), "# Sentence pair (%d)\n", num_sent );

		// line2 tgt
		for( int i=0; i<giza.tgt.tok_list.GetSize(); i++ )	{
			if( i > 0 )	{
				fprintf( out_file.GetFP(), " " );
			}
			fprintf( out_file.GetFP(), "%s", giza.tgt.tok_list.GetAt(i) );
		}
		fprintf( out_file.GetFP(), "\n" );

		// line3 src
		for( int i=0; i<giza.src.tok_list.GetSize(); i++ )	{
			if( i != 0 )	{
				fprintf( out_file.GetFP(), " " );
			}

			fprintf( out_file.GetFP(), "%s ({", giza.src.tok_list.GetAt(i) );
			for( int j=0; j<align_list.GetSize(); j++ )	{
				CUString align = align_list.GetAt(j);
				CUStringList st;
				st.TokenizeByChar( align, "-", false );
				int sid = atoi( st.GetAt(0) );
				int tid = atoi( st.GetAt(1) );

				if( sid == i )	{
					fprintf( out_file.GetFP(), " %d", tid );
				}

			}
			fprintf( out_file.GetFP(), " })" );
		}
		fprintf( out_file.GetFP(), "\n" );

		//fprintf( out_file.GetFP(), "%s\n", line.GetStr() );
		*/

	}

	fprintf( stdout, "num_sent\t%d\n", num_sent );
	fprintf( stdout, "num_cross\t%d\n", num_cross );
	fprintf( stdout, "num_align\t%d\n", num_align );
	return true;
}

int main( int argc, char *argv[] )
{
	if( argc != 2 )	{
		usage();
		return 1;
	}

	if( convert( argv[1] ) == false )	{
		return 1;
	}

	return 0;
}
